package com.lansi.blegatewayconfig.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lansi.bean.ResponseResult;
import com.lansi.blegatewayconfig.bean.BleGatewayConfig;
import com.lansi.blegatewayconfig.service.IBleGatewayConfigService;

@Controller
@RequestMapping("/lansi")
public class BleGatewayConfigController {
	//依赖注入
	@Resource
	private IBleGatewayConfigService bleGatewayConfigService;
	
	//l蓝牙网关配置注册
	@RequestMapping("/registerBleGateway.do")
	@ResponseBody
	public ResponseResult<Void> registerBleGateway(
			String deveui,
			String sn, 
			Byte adr,
			Byte rfu, 
			Byte dr, 
			Byte mode,
			Byte smode, 
			Byte power,
			Byte lostpoint,
			Byte selfadapt,
			Byte oneoff, 
			Byte alreport, 
			Short gps,
			Short hb,
			Short crc16
			){
		ResponseResult<Void> rr = new ResponseResult<Void>();
		try {
			System.out.println("蓝牙网关配置注册:");
			BleGatewayConfig bleGatewayConfig = new BleGatewayConfig();
			if(adr==null)   {adr=0;}
			if(rfu==null)   {rfu=0;}
			if(dr==null)    {dr=4;}
			if(mode==null)  {mode=3;}
			if(smode==null) {smode=0;}
			if(power==null) {power=20;}
			if(lostpoint==null) {lostpoint=0;}
			if(selfadapt==null)   {selfadapt=0;}
			if(oneoff==null)    {oneoff=0;}
			if(alreport==null)  {alreport=0;}
			if(gps==null) 		{gps=2;}
			if(hb==null) 		{hb=10;}
			if(crc16==null) 	{crc16=0;}			
			
			bleGatewayConfig.setDeveui(deveui);
			bleGatewayConfig.setSn(sn);			
			bleGatewayConfig.setAdr(adr);			
			bleGatewayConfig.setRfu(rfu);			
			bleGatewayConfig.setDr(dr);			
			bleGatewayConfig.setMode(mode);			
			bleGatewayConfig.setSmode(smode);			
			bleGatewayConfig.setPower(power);			
			bleGatewayConfig.setLostpoint(lostpoint);
			bleGatewayConfig.setSelfadapt(selfadapt);
			bleGatewayConfig.setOneoff(oneoff);
			bleGatewayConfig.setAlreport(alreport);
			bleGatewayConfig.setGps(gps);
			bleGatewayConfig.setHb(hb);
			bleGatewayConfig.setCrc16(crc16);
			bleGatewayConfigService.registerBleGatewayConfig(bleGatewayConfig);
			
			rr.setState(1);
			rr.setMessage("注册成功");
		} catch (RuntimeException e) {
			rr.setState(-1);
			rr.setMessage(e.getMessage());		
		}
		return rr;
		
	}
	
	//删除蓝牙配置--根据id
	@RequestMapping("/delBleGateway.do")
	@ResponseBody
	public ResponseResult<Void> delBleGateway(Integer[] ids){
		ResponseResult<Void> rr = new ResponseResult<Void>();
		try {
			System.out.println("删除人员个数:"+ids.length);
			if(ids.length>1) {
				bleGatewayConfigService.delBleGatewayConfig(ids);
				rr.setState(1);
				rr.setMessage("删除成功");
			}else {
				Integer id = ids[0];
				bleGatewayConfigService.delBleGatewayConfigById(id);
				rr.setState(1);
				rr.setMessage("删除成功");
			}
			
		} catch (RuntimeException e) {
			rr.setState(-1);
			rr.setMessage(e.getMessage());
		}
		return rr;		
	}
	
	//修改蓝牙网关配置
	@RequestMapping("/updateBleGateway.do")
	@ResponseBody
	public ResponseResult<Void> updateBleGateway(
			HttpSession session,
			Integer id,
			String deveui,
			String sn, 
			Byte adr,
			Byte rfu, 
			Byte dr, 
			Byte mode,
			Byte smode, 
			Byte power,
			Byte lostpoint,
			Byte selfadapt,
			Byte oneoff, 
			Byte alreport, 
			Short gps,
			Short hb,
			Short crc16
			
			){
		ResponseResult<Void> rr = new  ResponseResult<Void>();
		/*Manager manager = (Manager)session.getAttribute("user");
		String prio = manager.getPrio();*/
		if("1".equals("1")) {
			try {
				bleGatewayConfigService.updateBleGatewayConfig(id, deveui, sn, adr, rfu, dr, mode, smode, power, lostpoint, selfadapt, oneoff, alreport, gps, hb, crc16);
				rr.setState(1);
				rr.setMessage("修改成功");
			} catch (RuntimeException e) {
				rr.setState(-1);
				rr.setMessage(e.getMessage());
			}
		}else {
			rr.setState(-1);
			rr.setMessage("没有权限修改");
		}
		
		return rr;		
	}
	
	//修改全部蓝牙网关配置
	@RequestMapping("/updateAllBleGateway.do")
	@ResponseBody
	public ResponseResult<Void> updateAllBleGateway(
			String sn,  
			Byte adr,
			Byte rfu, 
			Byte dr, 
			Byte mode,
			Byte smode, 
			Byte power,
			Byte lostpoint,
			Byte selfadapt,
			Byte oneoff, 
			Byte alreport, 
			Short gps,
			Short hb,
			Short crc16
			){
		ResponseResult<Void> rr = new  ResponseResult<Void>();
		try {
			bleGatewayConfigService.updateAllBleGatewayConfig(null, sn, adr, rfu, dr, mode, smode, power, lostpoint, selfadapt, oneoff, alreport, gps, hb, crc16);
			rr.setState(1);
			rr.setMessage("修改成功");
		} catch (RuntimeException e) {
			rr.setState(-1);
			rr.setMessage(e.getMessage());
		}
		return rr;		
	}
	
	//根据条件查询蓝牙网关的配置情况
	@RequestMapping("/getBleGateway.do")
	@ResponseBody
	public ResponseResult<List<BleGatewayConfig>> getBleGateway(
			Integer page,
			String deveui,
			String sn
			){
		ResponseResult<List<BleGatewayConfig>> rr = new  ResponseResult<List<BleGatewayConfig>>();
		try {
			if(page==null) {
				page=1;
			}
			Integer offset = (page-1)*10;//跳过的页数
			List<BleGatewayConfig> list = bleGatewayConfigService.getBleGatewayConfig(deveui, sn, offset, 10);
			System.out.println("查询到的设备:"+list);
			Integer bleGatewayCongifcount = bleGatewayConfigService.getBleGatewayConfigcount(deveui, sn);
			System.out.println("查询到设备的数量:"+bleGatewayCongifcount);
			rr.setData(list);
			rr.setState(1);
			rr.setMessage(bleGatewayCongifcount.toString());
		} catch (RuntimeException e) {
			rr.setMessage(e.getMessage());
			rr.setState(-1);			
		}
		return rr;
		
	}
	
}
